package com.ms.student.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.ms.student.model.Student;
import com.ms.student.service.StudentService;
import lombok.extern.slf4j.Slf4j;

/**
* @comments
* @author  zg
* @date 2021年7月5日
*/
@Slf4j
@Controller
@RequestMapping("/student/")
public class StudentController {

    @Autowired
    private StudentService studentService;
    
    @RequestMapping("list")
    public String doFindStu(Model model) {
        log.info("查询学生列表：");
        List<Student> list = studentService.list();
        for (Student stu : list) {
            log.info("id={}, name={}, gender={}, cttime={}, uptime={}"
                    , stu.getId(), stu.getName(), stu.getGender(), stu.getCttime(), stu.getUptime());
        }
        model.addAttribute("stu", list);
        return "list";
    }
    
    @GetMapping("add")
    public String add(Model model) {
       
        return "add";
    }
    
    @PostMapping("add/submit")
    public String addSubmit(Student stu) {
        log.info("添加学生信息：{}", JSON.toJSONString(stu));
        studentService.add(stu);
        return "redirect:/student/list";
    }
    
    @RequestMapping("delete/{id}")
    public String doFindStu(@PathVariable Integer id) {
        log.info("删除学生信息：id={}", id);
        studentService.delete(id);
        return "redirect:/student/list";
    }
    
    @RequestMapping("update/{id}")
    public String update(@PathVariable Integer id, Model model) {
        Student stu = studentService.getById(id);
        model.addAttribute("stu", stu);
        return "update";
    }
    
    @RequestMapping("update/submit")
    public String updateSubmit(Student stu, Model model) {
        log.info("更新学生信息：{}", JSON.toJSONString(stu));
        studentService.updateById(stu);
        return "redirect:/student/list";
    }
}
